package services

import (
	"context"
	"my-gin-tiktok/app/models"
	"my-gin-tiktok/global"
)

type relationService struct{}

var RelationService = new(relationService)

// CreateRelation 建立关注
func (s *relationService) CreateRelation(ctx context.Context, fromID, toID int64) error {
	relation := models.Relation{UserID: fromID, ToUserID: toID}
	return global.App.DB.WithContext(ctx).Create(&relation).Error
}

// DeleteRelation 取消关注
func (s *relationService) DeleteRelation(ctx context.Context, fromID, toID int64) error {
	return global.App.DB.WithContext(ctx).Where("user_id = ? and to_user_id = ?", fromID, toID).Delete(&models.Relation{}).Error
}

// GetFollowList 我关注的列表
func (s *relationService) GetFollowList(ctx context.Context, fromID int64) ([]*models.Relation, error) {
	var relations []*models.Relation
	err := global.App.DB.WithContext(ctx).Where("user_id = ?", fromID).Find(&relations).Error
	return relations, err
}

// GetFollowerList 关注我的列表
func (s *relationService) GetFollowerList(ctx context.Context, toID int64) ([]*models.Relation, error) {
	var relations []*models.Relation
	err := global.App.DB.WithContext(ctx).Where("to_user_id = ?", toID).Find(&relations).Error
	return relations, err
}
